﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(CActionnerData))]
    public class CActionner: CComponent
    {
        #region identifiers

        public delegate void OnActivationEventHandler();

        #endregion

        #region fields

        private bool m_isActivated = false;
        private bool m_activateOnClick = false;
        private bool m_activateOnRelease = true;
        private CMouse.EButton m_activationButton = CMouse.EButton.LEFT;
        public event OnActivationEventHandler OnActivation;
        public event OnActivationEventHandler OnDesactivation;

        #endregion

        #region properties

        public virtual bool IsActivated { get { return m_isActivated; } }

        public virtual bool ActivateOnClick { get { return m_activateOnClick; } set { m_activateOnClick = value; } }
        public virtual bool ActivateOnRelease { get { return m_activateOnRelease; } set { m_activateOnRelease = value; } }
        public virtual CMouse.EButton ActivationButton { get { return m_activationButton; } set { m_activationButton = value; } }

        [ContentSerializerIgnore]
        public override CGameObject GameObject
        {
            get
            {
                return base.GameObject;
            }
            set
            {
                if (m_gameObject != null)
                {
                    m_gameObject.OnClick -= OnClick;
                    m_gameObject.OnRelease -= OnRelease;
                }
                base.GameObject = value;
            }
        }

        #endregion

        #region methods

        public override CComponent Clone(CGameObject gameObject)
        {
            CActionner act = new CActionner();
            act.GameObject = gameObject;
            act.m_activateOnClick = m_activateOnClick;
            act.m_activateOnRelease = m_activateOnRelease;
            act.m_activationButton = m_activationButton;
            act.OnActivation = OnActivation;
            act.OnDesactivation = OnDesactivation;
            return act;
        }

        public override void Initialize(CGameObject gameObject)
        {
            m_gameObject.OnClick += OnClick;
            m_gameObject.OnRelease += OnRelease;
            
        }

        public virtual void OnClick(CGameObject gameObject, CMouse.EButton button)
        {
            if (m_activateOnClick)
                if (button == m_activationButton)
                {
                    m_isActivated = !m_isActivated;
                    if (m_isActivated)
                    {
                        if (OnActivation != null)
                            OnActivation.Invoke();
                    }
                    else if (OnDesactivation != null)
                        OnDesactivation.Invoke();
                }
        }

        public virtual void OnRelease(CGameObject gameObject, CMouse.EButton button)
        {
            if (m_activateOnRelease)
                if (button == m_activationButton)
                {
                    m_isActivated = !m_isActivated;
                    if (m_isActivated)
                    {
                        if (OnActivation != null)
                            OnActivation.Invoke();
                    }
                    else if (OnDesactivation != null)
                        OnDesactivation.Invoke();
                }
        }

        #endregion
    }
}
